﻿using System;
using System.IO;
using System.Windows.Forms;
using RemoteTestRunner.BusinessLogic;
using RemoteTestRunner.BusinessLogic.Views;

namespace RemoteTestRunner.FormsUI
{
    public partial class Options : Form, IOptionsView
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Options"/> class.
        /// </summary>
        public Options()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes the form.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Options_Load(object sender, EventArgs e)
        {
            // Load settings
            txtDataFile.Text = RtrSettings.DataPath;
            numTestTimeout.Value = RtrSettings.TestTimeoutSeconds;
            optRunTestsLocally.Checked = !RtrSettings.UseRunnerService;
            optRunTestsOnServer.Checked = RtrSettings.UseRunnerService;
            txtMSTestPath.Text = RtrSettings.MsTestPath;
            txtWorkingFolder.Text = RtrSettings.WorkingFolder;
            txtServiceUrl.Text = RtrSettings.RunnerServiceUrl;
        }

        /// <summary>
        /// Saves any changes and closes the form.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (optRunTestsLocally.Checked && !File.Exists(txtMSTestPath.Text))
            {
                // MSTest.exe was not found
                MessageBox.Show(
                    "The path to MSTest does not exist.  Please check the path.",
                    "MSTest Not Found",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            }
            else
            {
                // Save settings and close
                RtrSettings.DataPath = txtDataFile.Text;
                RtrSettings.TestTimeoutSeconds = (int)numTestTimeout.Value;
                RtrSettings.UseRunnerService = optRunTestsOnServer.Checked;
                RtrSettings.MsTestPath = txtMSTestPath.Text;
                RtrSettings.WorkingFolder = txtWorkingFolder.Text;
                RtrSettings.RunnerServiceUrl = txtServiceUrl.Text;

                try
                {
                    if (!Directory.Exists(RtrSettings.WorkingFolder))
                    {
                        // create the directory
                        Directory.CreateDirectory(RtrSettings.WorkingFolder);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error creating working folder: " + RtrSettings.WorkingFolder + "\n" + ex.Message,
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                RtrSettings.Save();

                DialogResult = DialogResult.OK;
                Close();
            }
        }

        /// <summary>
        /// Enables/disables controls based on form state.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void runTests_CheckedChanged(object sender, EventArgs e)
        {
            lblServiceUrl.Enabled = optRunTestsOnServer.Checked;
            txtServiceUrl.Enabled = optRunTestsOnServer.Checked;

            lblMSTestPath.Enabled = optRunTestsLocally.Checked;
            txtMSTestPath.Enabled = optRunTestsLocally.Checked;
            btnBrowseMSTestPath.Enabled = optRunTestsLocally.Checked;

            lblWorkingFolder.Enabled = optRunTestsLocally.Checked;
            txtWorkingFolder.Enabled = optRunTestsLocally.Checked;
            btnBrowseWorkingFolder.Enabled = optRunTestsLocally.Checked;
        }

        /// <summary>
        /// Browses for the path of the MSTest.exe application.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnBrowseMSTestPath_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckPathExists = true;
            openFileDialog1.DefaultExt = ".exe";
            openFileDialog1.FileName = txtMSTestPath.Text;
            openFileDialog1.Filter =
                "MSTest (MSTest.exe)|MSTest.exe|Application Files (*.exe)|*.exe|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.Title = "Select MSTest Application";

            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtMSTestPath.Text = openFileDialog1.FileName;
            }
        }

        /// <summary>
        /// Browses for the path to the data file where the test configurations are stored.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnBrowseDataPath_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckPathExists = false;
            openFileDialog1.DefaultExt = ".xml";
            openFileDialog1.FileName = txtDataFile.Text;
            openFileDialog1.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.Title = "Select Data File";

            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtDataFile.Text = openFileDialog1.FileName;
            }
        }

        /// <summary>
        /// Browses to the working folder.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnBrowseWorkingFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "Select Working Folder";
            folderBrowserDialog1.SelectedPath = txtWorkingFolder.Text;
            folderBrowserDialog1.ShowNewFolderButton = true;

            if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtWorkingFolder.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}